package al.work.test.aop;

import al.work.httplisten.aop.JoinPoint;
import al.work.httplisten.aop.annotation.*;
import al.work.test.data.ResultVo;

/**
 * 全局异常处理
 * @author JiuZh
 */
@Aspect
@ControllerException
public class GlobalExceptionHandler {

    @ExceptionHandler(value = NullPointerException.class)
    public ResultVo<Object> handleNullException(Exception ex) {
        Integer resultCode = 100;
        String errorInfo = "空指针异常";
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setFlag(resultCode);
        resultVo.setInfo(errorInfo);
        return resultVo;
    }
    @ExceptionHandler(value = Exception.class)
    public ResultVo<Object> FinalException(Exception ex) {
        Integer resultCode = 100;
        String errorInfo = "异常";
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setFlag(resultCode);
        resultVo.setInfo(ex.toString());
        return resultVo;
    }

    @Before("@annotation(al.work.test.aop.ParamValidate)")
    @Order(-1)
    public void before1(JoinPoint joinPoint) {
        int i = 0;
        i += 4;
    }

    @Before("@annotation(al.work.test.aop.ParamValidate)")
    public void before2(JoinPoint joinPoint) {
        int i = 0;
        i += 4;
    }

    @After("@annotation(al.work.test.aop.ParamValidate)")
    public void after1(JoinPoint joinPoint) {
        int i = 0;
        i += 4;
    }

    @After("@annotation(al.work.test.aop.ParamValidate)")
    @Order
    public void after2(JoinPoint joinPoint) {
        int i = 0;
        i += 4;
    }

    @AfterReturning(value = "@annotation(al.work.test.aop.ParamValidate)", returning = "retVl")
    public void afterreturn1(JoinPoint joinPoint, Object retVl) {
        int i = 0;
        i += 4;
    }

    @AfterReturning("@annotation(al.work.test.aop.ParamValidate)")
    @Order(3)
    public void afterreturn2(JoinPoint joinPoint) {
        int i = 0;
        i += 4;
    }

    @AfterThrowing(value = "@annotation(al.work.test.aop.ParamValidate)", throwing = "object")
    @Order(3)
    public void afterthrow1(JoinPoint joinPoint, Object object) {
        int i = 0;
        i += 4;
    }

    @AfterThrowing(value = "@annotation(al.work.test.aop.ParamValidate)", throwing = "object")
    @Order(2)
    public void afterthrow2(JoinPoint joinPoint, Object object1) {
        int i = 0;
        i += 4;
    }

    @ExceptionHandler(value = Exception.class)
    public void handleException(Exception e) {
        e.printStackTrace();
    }
}
